﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace Jemgine.MapEditor.Controls
{
	public partial class ResourcePreview : GraphicsDeviceControl
	{
        IResourcePreview _previewer = null;
        ContentManifestExtensions.ContentType ResourceType;

        string _selectedResource;
        internal String SelectedResource
        {
            set
            {
                _selectedResource = value;
                if (Initialized) _previewer.LoadResource(GraphicsDevice, EditorState.Content, value);
            }
        }

        EditorState EditorState;
        ContentManager LocalContent;
        System.Threading.Timer Timer;
        Jemgine.Engine.Renderer.Camera Camera = new Engine.Renderer.Camera(Vector2.One);
        bool Initialized = false;
        

		public ResourcePreview()
		{
			InitializeComponent();
		}

        internal void Initialize(EditorState _editorState, ContentManifestExtensions.ContentType _resourceType,
            String _initialResource)
        {
            EditorState = _editorState;
            ResourceType = _resourceType;
            _selectedResource = _initialResource;
        }


        protected override void GDCInitialize()
		{
            LocalContent = new ContentManager(Services);

           	base.GDCInitialize();

            Timer = new System.Threading.Timer((A) => Invalidate(), null, TimeSpan.FromSeconds(0),
                TimeSpan.FromSeconds(1.0f / 120.0f));



            IResourcePreview previewer = null;
            switch (ResourceType)
            {
                case ContentManifestExtensions.ContentType.Texture:
                    previewer = new TexturePreview();
                    break;
                case ContentManifestExtensions.ContentType.ParticleEffect:
                    previewer = new EffectPreview();
                    break;
                default:
                    break;
            }

            _previewer = previewer;
            if (previewer != null)
            {
                previewer.Initialize(GraphicsDevice, LocalContent);
                previewer.LoadResource(GraphicsDevice, EditorState.Content, _selectedResource);
            }
            Initialized = true;
		}

        int LastDraw = System.Environment.TickCount;

        protected override void GDCDraw()
        {
            try
            {
                int ElapsedMilliseconds = System.Environment.TickCount - LastDraw;
                LastDraw = System.Environment.TickCount;

                if (_previewer != null) _previewer.Update((float)ElapsedMilliseconds / 1000.0f);
        
                Camera.viewportDimensions = new Vector2(Width, Height);
                Camera.zoom = 8;

                this.GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.Black);

                if (_previewer != null) _previewer.Draw(GraphicsDevice, Camera);

                base.GDCDraw();
            }
            catch (Exception e)
            {
                Timer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
                MessageBox.Show(e.Message);
                Timer.Change(TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(1.0f / 120.0f));
            }
        }

		private void MapRenderControl_SizeChanged(object sender, EventArgs e)
		{
            Camera.viewportDimensions = new Vector2(Width, Height);
		}
        
		private void MapRenderControl_MouseDown(object sender, MouseEventArgs e)
		{
            if (_previewer != null) _previewer.MouseDown(Camera.UnProject(new Vector2(e.X, e.Y)));
		}

		private void MapRenderControl_MouseUp(object sender, MouseEventArgs e)
		{
            if (_previewer != null) _previewer.MouseUp(Camera.UnProject(new Vector2(e.X, e.Y)));
        }

		private void MapRenderControl_MouseMove(object sender, MouseEventArgs e)
		{
            if (_previewer != null) _previewer.MouseMove(Camera.UnProject(new Vector2(e.X, e.Y)));
        }
    }
}
